/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.kth.lwr.jeat.samples.onsitetreatment.producers;

import javax.measure.unit.SI;
import javax.measure.unit.Unit;
import org.jscience.physics.amount.Amount;
import se.kth.lwr.jeat.core.*;
import se.kth.lwr.jeat.utils.*;
import se.kth.lwr.jeat.samples.onsitetreatment.SimpleProduction;

/**
 * Produces polyeten pipes with a diameter of 32 mm
 *
 * @author davidev
 */
public class PolyEtenPipeProducer extends SimpleProduction {
    
    //Data sources

    /**
     * Density of 32 mm PEM pipes
     * <p/>
     * Value: 0.5
     * <p/>
     * Unit: kg/m
     */
    public static final AmountDataSource pemPipeMetricDensity = new AmountDataSource<>(
            "metric density 32 mm PEM pipe",
            "Density of 32 mm PEM pipes",
            Amount.valueOf(0.5, Unit.valueOf("kg/m")));

    /**
     *
     * Resouce consumtions during production of PolyEten (Boustead (1993)
     *
     */
    public static final InventoryDataSource polyEtenInventoryDataSource = new InventoryDataSource("PolyEtenProducerEmissions",
            "Resouce consumtions during production of PolyEten (Boustead (1993)",
            new Inventoriable() {

                @Override
                public Inventory getInventory() {
                    Inventory retval = Inventory.emptyInventory();
                    retval.addEnergyUse(EnergyCategory.FOSSIL, Amount.valueOf(77.94, Unit.valueOf("MJ/kg")));
                    retval.addEnergyUse(EnergyCategory.ELECTRICAL, Amount.valueOf(7.89, Unit.valueOf("MJ/kg")));
                    return retval;
                }
            });
    

    private PolyEtenPipeProducer(ProductionSupport support) {
        super(support);
    }

    public static PolyEtenPipeProducer getInstance() {
        AmountConverter manager = new PEAmountManager(SI.KILOGRAM);
        ProductionSupport support = new ProductionSupport("Polyeten", polyEtenInventoryDataSource, manager);
        return new PolyEtenPipeProducer(support);
    }

    static class PEAmountManager extends DefaultAmountConverter {

        public PEAmountManager(Unit acceptable) {
            super(acceptable);
        }

        @Override
        public AmountFunction toCompatibleAmount(AmountDataSource amount) {
            if (amount.readValue().getUnit().isCompatible(SI.METER)) {
                return new AmountFunction().multiply(amount).multiply(pemPipeMetricDensity);

            } else {
                return super.toCompatibleAmount(amount);
            }
        }
    }
}
